package com.tengju.data.infrastructure.service;

import com.tengju.data.domain.model.report.DataNameEnum;
import com.tengju.data.domain.model.report.DataVersionRepository;
import com.tengju.data.domain.model.service.DataVersionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenhang
 * @Date 2021/2/20
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_={@Autowired})
public class DataVersionServiceImpl implements DataVersionService {

    private final DataVersionRepository dataVersionRepository;

    private boolean checkVersion(DataNameEnum dataName,Integer version) {
        Integer dataVersion = dataVersionRepository.getDataVersion(dataName);
        return dataVersion != null && dataVersion >= version;
    }

    @Override
    public void saveVersion(DataNameEnum dataName, Integer version) {
        dataVersionRepository.saveVersion(dataName,version);
    }

    /**
     * 校验星级快照是否完成
     * @param version eg:202101
     * @return
     */
    @Override
    public boolean checkStarVersion(Integer version) {
        return checkVersion(DataNameEnum.USER_STAR,version);
    }

    /**
     * 校验黑卡关系快照是否完成
     * @param version eg:202101
     * @return
     */
    @Override
    public boolean checkRelationVersion(Integer version) {
        return checkVersion(DataNameEnum.RELATION,version);
    }

    /**
     * 校验出货额快照快照是否完成
     * @param version eg:202101
     * @return
     */
    @Override
    public boolean checkAchievementVersion(Integer version) {
        return checkVersion(DataNameEnum.ACHIEVEMENT,version);
    }

    /**
     * 校验大数据星级变更提醒是否完成
     * @param version eg:20210101
     * @return
     */
    @Override
    public boolean checkStarNoticeVersion(Integer version) {
        return checkVersion(DataNameEnum.STAR_NOTICE,version);
    }

    /**
     * 校验星级变更邀请是否完成
     * @param version eg:202101
     * @return
     */
    @Override
    public boolean checkStarInviteVersion(Integer version) {
        return checkVersion(DataNameEnum.STAR_INVITE,version);
    }

    /**
     * 校验风控对比-出货额计算是否完成
     * @param version
     * @return
     */
    @Override
    public boolean checkAchievementRiskVersion(Integer version) {
        boolean result = checkVersion(DataNameEnum.ACHIEVEMENT_RISK, version);
        if (!result) {
            log.error("上游数据表:{},version:{}未准备好!",DataNameEnum.ACHIEVEMENT_RISK.getName(),version);
        }
        return result;
    }


}
